<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script>
var cardNewType = "";
var flag = "";
$(function () {
    $.ajaxSetup({ cache: false });
    flag = $("#flag").val();
    if (flag == "1") {
        $("#cardStatus").val(0).trigger("change");
        $("#cardStatus").attr("disabled", "true");
    }
    else if (flag == "2") {
        $("#idCard").attr("disabled", "disabled");
        $("#rechangeType").attr("data-parsley-required", false);
        $("#deposit").attr("data-parsley-required", false);
        $("#rechangeTypeDiv").attr("display", "none");
        $("#readIdCard").attr("disabled", "disabled");
        $("#deposit").attr("disabled", "disabled");
        $("#cardStatus").attr("disabled", "true");
        $("#rechangeType").attr("disabled", "disabled");
        $("#balance").attr("disabled", "disabled");
        $("#deposit").val(0);
        toastr.error("请先读取卡片信息");
    } else if (flag == "3") {
        toastr.error("请先读取卡片信息");
        $("#readIdCard").attr("disabled", "disabled");
        $("#cardStatus").attr("disabled", "disabled");
        $("#idCard").attr("disabled", "disabled");
        $("#name").attr("disabled", "disabled");
        $("#age").attr("disabled", "disabled");
        $("#phone").attr("disabled", "disabled");
        $("#homeplace").attr("disabled", "disabled");
        $("#nation").attr("disabled", "disabled");
        $("#name").attr("disabled", "disabled");
        $("#balance").attr("disabled", "disabled");
        $("#sex").attr("disabled", "disabled");
        $("#expand1").attr("disabled", "disabled");
     } else if (flag == "4") {
        $("#balanceAdd").show();
    } else if (flag == "5") {
        $("#readIdCard").attr("disabled", "disabled");
        $("#cardStatus").attr("disabled", "disabled");
        $("#idCard").attr("disabled", "disabled");
        $("#name").attr("disabled", "disabled");
        $("#age").attr("disabled", "disabled");
        $("#phone").attr("disabled", "disabled");
        $("#homeplace").attr("disabled", "disabled");
        $("#nation").attr("disabled", "disabled");
        $("#name").attr("disabled", "disabled");
        $("#balance").attr("disabled", "disabled");
        $("#sex").attr("disabled", "disabled");
        $("#expand1").attr("disabled", "disabled");
        $("#rechangeType").attr("disabled","disabled");
        $("#deposit").attr("disabled", "disabled");
         $("#readIdCard").attr("disabled", "disabled");
    }
    // 读取身份证
    $("#readIdCard").on("click", function () {
        var str = FindReader_onclick();
        if (str == "1") {
            toastr.error("没有找到读卡器！");
            return false;
        } else {
            //找到读卡器
            ReadCard_onclick();//写入数据
            $("#readIdCard").attr("disabled", "disabled");
            $("#idCard").attr("disabled", "disabled");
            $("#name").attr("disabled", "disabled");
            $("#homeplace").attr("disabled", "disabled");
            $("#nation").attr("disabled", "true");
            $("#backCard").attr("disabled", "disabled");
            $("#carteVital").attr("disabled", "disabled");
            var idCard = $("#idCard").val();
            if (!idCard) {
                toastr.error("读卡失败");
                return false;
            }
        }
    })
    $("#readOneCard").on("click", function () {
        var cardNo = readOneCard();
        if (!cardNo) {
            toastr.error("读卡失败");
            return false;
        } else {
            $("#cardNo").val(cardNo);
            var flag = $("#flag").val();
            if (flag == 3 || flag == 2 || flag == 5) {
                $.ajax({
                    type: "post",
                    url: timestamp("${ctx}/med/medicalCardMaster/findByCardNo?cardNo=" + cardNo),
                    cache:false,
                    dataType: "json",
                    success: function (data) {
                        $("#id").val(data.id);
                        $("#patientId").val(data.patientId);
                        $("#cardStatus option[value=" + data.cardStatus + "]").attr("selected", "selected");
                        $("#cardStatus").change();
                        $("#rechangeType option[value=" + data.rechangeType + "]").attr("selected", "selected");
                        $("#rechangeType").change();
                        $("#idCard").val(data.idCard);
                        $("#name").val(data.name);
                        $("#age").val(data.age);
                        $("#phone").val(data.phone);
                        $("#homeplace").val(data.homeplace);
                        $("#sex option[value=" + data.sex + "]").attr("selected", "selected");
                        $("#sex").change();
                         $("#expand1 option[value=" + data.expand1 + "]").attr("selected", "selected");
                        $("#expand1").change();
                         $("#deposit").val("");
                        if (flag == 2) {
                            $("#deposit").val(0);
                        }
                        if(flag == 5){
                            $("#rechangeType option[value='-1']").attr("selected", "selected");
                            $("#rechangeType").change();
                            $("#deposit").val(0);
                            toastr.error("请退还用户"+data.balance+"元");
                        }
                        $("#balance").val(data.balance);
                    }, error: function (data) {
                        toastr.error('网络连接错误,请检查网络');
                    }
                });

            }
            toastr.success("读卡成功！");
        }
    })


    $("#btnCancel").on('click', function () {
        hideShowTableTd('contentTable', '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14', '1');
        $("#RightDiv").attr("style", "display: none");
        $("#LeftDiv").attr("class", "main-content col-lg-12");
        $("#LeftDiv").show('normal');
    })

    //切换充值支付方式
    $("#rechangeType").change(function () {
        var rechangeType = $(this).val();
        if (rechangeType == 0) {
            $("#depositDiv").show();
            $("#deposit").attr("data-parsley-required", true);
        } else {
            $("#depositDiv").hide();
            $("#deposit").val(0);
            $("#deposit").attr("data-parsley-required", false);
        }
    })
    //余额合并
    $("#isRechange").change(function () {
        var isRechange = $(this).val();
        if (isRechange == 0) {
            //合并
            // 1 查询挂失卡的信息
            var idCard = $("#idCard").val();
            if (!idCard) {
                toastr.error("请先读取省份证信息,获取挂失卡信息");
                return false;
            }
            $.ajax({
                type: "post",
                url: "${ctx}/med/medicalCardMaster/findByIdCard?idCard=" + idCard,
                dataType: "json",
                success: function (data) {
//                        alert("success");
                    //返回就卡对象
                    if (data.length == 0) {
                        toastr.error('没有找到余额大于0的已经挂失的卡信息');
                        return false;
                    }
                    $("#oldBalanceDiv").show();
                    var oldIds = "";
                    var oldBalance = "";
                    for (var i = 0; i < data.length; i++) {
                        oldIds += data[i].id + ","
                        oldBalance += data[i].balance;
                    }
                    oldIds = oldIds.substring(0, oldIds.length - 1);
                    $("#oldId").val(data.id);
                    $("#oldBalance").val(oldBalance);//挂失卡余额
                }, error: function (data) {
                    toastr.error('网络连接错误,请检查网络');
                }
            });

        } else {

        }
    })




    /**
    * 表单提交
     */
    var flag=true;
    $("#btnSave").on('click',function(){
        var idCard=$("#idCard").val();
        if(idCard!=null&&idCard!=""){
            var age=getAge(idCard);
            if(!age){
                toastr.error("输入的省份证号码不合法！");
                return false;
            }else{
                $("#age").val(getAge(idCard));
            }
        }
        if ($('#inputForm').parsley().validate()) {
            if(flag){
                flag=false;
                formSubmitInput("inputForm");
                $("#" + "inputForm" + " input,#" + "inputForm" + " select").removeAttr("disabled");
                $("#" + "inputForm").ajaxSubmit({
                    type: 'post', // 提交方式 get/post
                    url:'${ctx}/med/medicalCardMaster/save', // 需要提交的 url
                    success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                        if (data != null) {
                            toastr.success(data.data);
                            loadDiv('mainCenterDiv', '${ctx}/med/medicalCardMaster/index');
                            var flag=$("#flag").val();
                            var deposit=parseInt($("#deposit").val());
                            var balance=data.code;
                            if((flag=="1" || flag=="3")&&deposit>0){
                                var name=$("#name").val();
                                var pdata={deposit:deposit,name:name,balance:balance};
                                printPdf('${ctx}/med/medicalCardMaster/printDeposit',pdata);
                            }
                        }
                    }
                });
            }
         }
     })
});

function getAge(idNO){
    var myDate = new Date();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var age = myDate.getFullYear() - idNO.substring(6, 10) - 1;
    if (idNO.substring(10, 12) < month || idNO.substring(10, 12) == month && idNO.substring(12, 14) <= day) {
        age++;
    }
    return age;
}
 </script>


<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="col-lg-12">
                    <form:form id="inputForm" data-parsley-validate="" modelAttribute="medicalCardMaster" method="post"
                               class="form-horizontal"
                               onsubmit="return formSaveLoad('mainCenterDiv','inputForm','${ctx}/med/medicalCardMaster/save','${ctx}/med/medicalCardMaster/index')">
                        <form:hidden path="cardType" value="0"/>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <input class="btn btn-primary" type="button" value="读取身份证"
                                       style="background-color:#15db81;" id="readIdCard"/>
                                <input class="btn btn-primary" type="button" value="读取一卡通" id="readOneCard"
                                       style="background-color:#698B69;"/>
                            </div>
                        </div>
                        <form:hidden path="patientId"/>
                        <form:hidden path="flag"/>
                        <form:hidden path="id"/>
                        <form:hidden path="oldId"/>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:input path="cardNo" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="卡号" data-parsley-required="true"
                                            disabled="true"/>
                            </div>
                        </div>

                        <div class="form-group" id="balanceAdd" style="display: none;">
                            <div class="col-sm-12">
                                <form:select path="isRechange" itemLabel="label" itemValue="value" class="form-control">
                                    <form:option value="" label="余额合并"/>
                                    <form:option value="0" label="是" selected="selected"/>
                                    <form:option value="1" label="否"/>
                                </form:select>
                            </div>
                        </div>

                        <div class="form-group" style="display: none;" id="oldBalanceDiv">
                            <div class="col-sm-12">
                                <form:input path="oldBalance" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="挂失卡余额" disabled="disabled"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:select path="cardStatus" itemLabel="label" itemValue="value" class="form-control"
                                             data-parsley-required="true">
                                    <form:option value="" label="卡状态"/>
                                    <form:options items="${fns:getDictList('card_status')}" itemLabel="label"
                                                  itemValue="value" htmlEscape="false"/>
                                </form:select>
                            </div>
                        </div>


                        <div class="form-group" id="rechangeTypeDiv">
                            <div class="col-sm-12">
                                <form:select path="rechangeType" itemLabel="label" itemValue="value"
                                             class="form-control" data-parsley-required="true">
                                    <form:options items="${fns:getDictList('card_pay_way')}" itemLabel="label"
                                                  itemValue="value" htmlEscape="false"/>
                                    <form:option value="-1" label="不充值"/>
                                </form:select>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:input path="idCard" htmlEscape="false" class="form-control" data-parsley-checkidcard="3"
                                            maxlength="20" placeholder="身份证号码" data-parsley-required="true"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:input path="name" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="姓名" data-parsley-required="true"/>
                            </div>
                        </div>
                        <div class="form-group" style="display: none;">
                            <div class="col-sm-12">
                                <form:input path="age" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="年龄" data-parsley-required="true"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:input path="homeplace" htmlEscape="false" class="form-control"
                                            maxlength="100" placeholder="出生地"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:select path="nation" itemLabel="label" itemValue="value" class="form-control">
                                    <form:option value="" label="民族"/>
                                    <form:options items="${fns:getDictList('NATION_DICT')}" itemLabel="label"
                                                  itemValue="value" htmlEscape="false"/>
                                </form:select>
                            </div>
                        </div>

                        <div class="form-group" id="depositDiv">

                            <div class="col-sm-12">
                                <label class=" control-label">充值金额</label>
                                 <form:input path="deposit" id="deposit" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="充值金额" data-parsley-required="true" min="0"/>
                            </div>
                        </div>
                        <div class="form-group">
                             <div class="col-sm-12">
                                <label class="control-label">卡&nbsp;余额</label>
                                <form:input path="balance" htmlEscape="false" class="form-control"
                                            maxlength="20" placeholder="卡余额" disabled="true"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:select path="expand1" itemLabel="label" itemValue="value" class="form-control"
                                             data-parsley-required="true">
                                    <form:option value="" label="费别"/>
                                    <form:options items="${fns:getDictList('CHARGE_TYPE_DICT')}" itemLabel="label"
                                                  itemValue="value" htmlEscape="false"/>
                                </form:select>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12">
                                <form:select path="sex" itemLabel="label" itemValue="value" class="form-control"
                                             data-parsley-required="true">
                                    <form:option value="" label="性别"/>
                                    <form:options items="${fns:getDictList('SEX_DICT')}" itemLabel="label"
                                                  itemValue="value" htmlEscape="false"/>
                                </form:select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <input id="btnSave" class="btn btn-primary" type="button" value="保存">&nbsp;
                                <input id="btnCancel" class="btn" type="button" value="关 闭"></div>

                        </div>
                    </form:form>
                    <input type="hidden" name="sexTemp" id="sexTemp"/>
                </div>
            </div>
         </section>
    </div>
</div>
